package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 用预编译SQL完成下面工作:
 * 根据用户名，修改用户的密码
 */
public class JDBCDemo101 {
    public static void main(String[] args) {
        System.out.println("修改密码");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入新密码:");
        String password = scanner.nextLine();

        String sql = "UPDATE user " +
                "SET password=? " +
                "WHERE username=?";

        try (Connection connection = DBUtil.getConnection();){
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,username);
            int num = ps.executeUpdate();
            System.out.println(num>0?"修改成功":"修改失败");

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}







